package cn.csc.module.distribute.dal.mysql.downline;

import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.mybatis.core.mapper.BaseMapperX;
import cn.csc.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.csc.framework.mybatis.core.query.LambdaUpdateWrapperX;
import cn.csc.module.distribute.controller.admin.downline.vo.DistributeDownlineExcelVO;
import cn.csc.module.distribute.controller.admin.downline.vo.DistributeDownlineExportReqVO;
import cn.csc.module.distribute.controller.admin.downline.vo.DistributeDownlinePageReqVO;
import cn.csc.module.distribute.controller.admin.downline.vo.DistributeDownlineRespVO;
import cn.csc.module.distribute.controller.app.downline.vo.AppDistributeDownlineReqVO;
import cn.csc.module.distribute.dal.dataobject.downline.DistributeDownlineDO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;

/**
 * 分销下线信息 Mapper
 *
 * @author liuchuang
 */
@Mapper
public interface DistributeDownlineMapper extends BaseMapperX<DistributeDownlineDO> {

    default PageResult<DistributeDownlineDO> selectPage(DistributeDownlinePageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<DistributeDownlineDO>()
                .eqIfPresent(DistributeDownlineDO::getMobile, reqVO.getMobile())
                .orderByDesc(DistributeDownlineDO::getId));
    }

    default PageResult<DistributeDownlineDO> selectAppPage(AppDistributeDownlineReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<DistributeDownlineDO>()
                .eq(DistributeDownlineDO::getParentId, reqVO.getParentId())
                .orderByDesc(DistributeDownlineDO::getId));
    }

    default List<DistributeDownlineDO> selectList(DistributeDownlineExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<DistributeDownlineDO>()
                .eqIfPresent(DistributeDownlineDO::getParentId, reqVO.getParentId())
                .eqIfPresent(DistributeDownlineDO::getChildId, reqVO.getChildId())
                .eqIfPresent(DistributeDownlineDO::getLevel, reqVO.getLevel())
                .likeIfPresent(DistributeDownlineDO::getNickname, reqVO.getNickname())
                .eqIfPresent(DistributeDownlineDO::getMobile, reqVO.getMobile())
                .eqIfPresent(DistributeDownlineDO::getOrderCount, reqVO.getOrderCount())
                .eqIfPresent(DistributeDownlineDO::getCommissionTotal, reqVO.getCommissionTotal())
                .betweenIfPresent(DistributeDownlineDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(DistributeDownlineDO::getId));
    }

    default DistributeDownlineDO selectByChildId(BigInteger childId){
        return selectOne(new LambdaQueryWrapperX<DistributeDownlineDO>()
                .eq(DistributeDownlineDO::getChildId, childId)
        );
    }

    /**
     * 计算下单人的金额信息
     *
     * @param
     * @param
     * @return
     */
    default int increFieldDownline(BigInteger parentId, BigInteger childId,Integer count,BigDecimal commissionTotal,BigDecimal orderAmount) {
        LambdaUpdateWrapperX<DistributeDownlineDO> updateWrapper = new LambdaUpdateWrapperX();
        updateWrapper.increField(DistributeDownlineDO::getOrderCount, count);
        updateWrapper.increField(DistributeDownlineDO::getOrderAmount, orderAmount);
        updateWrapper.increField(DistributeDownlineDO::getCommissionTotal, commissionTotal);
        updateWrapper.eq(DistributeDownlineDO::getChildId, childId);
        updateWrapper.eq(DistributeDownlineDO::getParentId, parentId);
        return update(null, updateWrapper);
    }
    /**
     * admin接口，分页列表
     * @param reqVO
     * @param page
     * @return
     */
    Page<DistributeDownlineRespVO> selectAdminPage(@Param("req") DistributeDownlinePageReqVO reqVO, IPage<DistributeDownlineRespVO> page);

    /**
     * 导出信息接口-包括分销人信息
     * @param reqVO
     * @return
     */
    List<DistributeDownlineExcelVO> getExportDownlineList(DistributeDownlinePageReqVO reqVO);
}
